package lolxxs.chenyanting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lolxxs.chenyanting.mapper.ResumeInformationMapper;
import lolxxs.chenyanting.model.Resume;
import lolxxs.chenyanting.mapper.ResumeMapper;
import lolxxs.chenyanting.model.ResumeInformation;
import lolxxs.chenyanting.service.IResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lolxxs.chenyanting.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2022-01-01
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements IResumeService {

    @Autowired
    private ResumeMapper  resumeMapper;

    @Autowired
    private ResumeInformationMapper resumeInformationMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public int update(Resume resume) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", resume.getUserId());
        System.out.println(resume);
       Resume resume1 = resumeMapper.selectOne(queryWrapper);
        if (resume1 != null) {
            resume.setResumeId(resume1.getResumeId());
            // 更新逻辑
            int update =  resumeMapper.updateById(resume);
            ResumeInformation resumeInformation = new ResumeInformation();
            resumeInformation.setUserId(resume.getUserId());
            resumeInformation.setResumeId(resume.getResumeId());
            Date date = new Date();
            resumeInformation.setUpdateTime(date);
            resumeInformation.setCreateTime(date);
            resumeInformation.setState(0);
            resumeInformation.setLevel(1);
            if (resume.getIntroduction() != null ) {
                if (resume.getIntroduction().length() > 12) {
                    resumeInformation.setSummary(resume.getIntroduction().substring(0, 12));
                }
                resumeInformation.setSummary(resume.getIntroduction());
            }
            resumeInformationMapper.updateById(resumeInformation);
            if (update > 0) {
                return 2;
            }
            return update;
        }
        resume.setResumeId(idWorker.nextId());
        ResumeInformation resumeInformation = new ResumeInformation();
        resumeInformation.setUserId(resume.getUserId());
        resumeInformation.setResumeId(resume.getResumeId());
        Date date = new Date();
        resumeInformation.setUpdateTime(date);
        resumeInformation.setCreateTime(date);
        resumeInformation.setState(0);
        resumeInformation.setLevel(1);
        if (resume.getIntroduction() != null ) {
            if (resume.getIntroduction().length() > 12) {
                resumeInformation.setSummary(resume.getIntroduction().substring(0, 12));
            }
            resumeInformation.setSummary(resume.getIntroduction());
        }
        resumeInformationMapper.insert(resumeInformation);
        // 插入逻辑
        int insert = resumeMapper.insert(resume);
        if (insert > 0) {
            return 1;
        }
        return insert;
    }

    @Override
    public Resume query(Resume resume) {
        Resume resume1 = resumeMapper.selectOne(queryWrapper(resume));
        if (resume1 != null) {
           return resume1;
        }
        return null;
    }

    private QueryWrapper<Resume> queryWrapper(Resume resume) {
        QueryWrapper<Resume> wrapper=new QueryWrapper<>();
        if (resume.getResumeId() != null) {
            wrapper.eq("resumeId", resume.getResumeId());
        }
        if (resume.getUserId() != null) {
            wrapper.eq("userId", resume.getUserId());
        }
        if (resume.getUserName() != null) {

            wrapper.eq("userName", resume.getUserName());
        }
        if (resume.getName() != null) {

            wrapper.eq("name", resume.getName());
        }
        if (resume.getAge() != null) {
            wrapper.eq("age", resume.getAge());
        }
        if (resume.getSex() != null) {
            wrapper.eq("sex", resume.getSex());//查询投简历者
        }
        if (resume.getTelephone() != null) {
            wrapper.eq("telephone", resume.getTelephone());
        }
        if (resume.getQq() != null) {
            wrapper.eq("qq", resume.getQq());
        }
        if (resume.getEmail() != null) {
            wrapper.eq("email", resume.getEmail());
        }
        if (resume.getWorkingPlace() != null) {
            wrapper.eq("workingPlace", resume.getWorkingPlace());
        }
        if (resume.getIntroduction() != null) {
            wrapper.like("introduction", resume.getIntroduction());
        }
        if (resume.getSkill() != null) {
            wrapper.like("skill", resume.getSkill());
        }
        if (resume.getThrough() != null) {
            wrapper.like("through", resume.getThrough());
        }
        if (resume.getEducation() != null) {
            wrapper.like("education", resume.getEducation());
        }
        if (resume.getHonor() != null) {// 荣耀
            wrapper.like("honor", resume.getHonor());
        }
        if (resume.getExperience() != null) {
            wrapper.like("experience", resume.getExperience());
        }
        return wrapper;
    }
}
